#!/bin/bash

prefix_core=":core_"
prefix_base=":base_"
core_array=()

echo "  "
echo "=========================================================="
echo "==== Looking for project to publish ..."
echo "=========================================================="

while read line
do
    result_base=$(echo $line | grep "${prefix_base}")
    if  [[ "$result_base" != "" ]]
    then
        project_name_t=${line//"include '"/""}
        project_name=${project_name_t//"'"/""}
        echo "found base -- $project_name"
        core_array[${#core_array[@]}]=$project_name
    fi
done < settings.gradle

while read line
do
    result_core=$(echo $line | grep "${prefix_core}")
    if  [[ "$result_core" != "" ]]
    then
        project_name_t=${line//"include '"/""}
        project_name=${project_name_t//"'"/""}

        # TODO：过滤两个工程(后面改掉：需要把这两个工程flavor抹除！)
        if  [[ "$project_name" != ":core_persistent_connection" ]]
        then
            if  [[ "$project_name" != ":core_message_push" ]]
            then
                echo "found core -- $project_name"
                core_array[${#core_array[@]}]=$project_name
            fi
        fi
    fi
done < settings.gradle

# 一些特性

core_array[${#core_array[@]}]=":feature_device_info"
core_array[${#core_array[@]}]=":feature_data_structure"
core_array[${#core_array[@]}]=":feature_backend_service"
core_array[${#core_array[@]}]=":feature_message_query"

# SDK壳工程
core_array[${#core_array[@]}]=":shell_core_integration"

echo "  "
echo "  "
echo "=========================================================="
echo "==== Size : ${#core_array[@]}"
for project_name in ${core_array[@]}
do
    echo "====        |____ $project_name"
done;
echo "=========================================================="

echo "  "
echo "  "
echo "=========================================================="
echo "==== Cleaning..."
echo "=========================================================="

./gradlew clean -q
if [ $? -ne 0 ];then
    echo "Error while cleaning $project_name"
    exit -1
fi

echo "  "
echo "  "
echo "=========================================================="
echo "==== Cleaning success !"
echo "=========================================================="


echo "  "
echo "  "
echo "=========================================================="
echo "==== Building all project ..."
echo "=========================================================="
echo "  "
echo "  "

########## 编译产物 ##########
for project_name in ${core_array[@]}
do
    echo "  "
    echo "  "
    echo "=========================================================="
    echo "==== Building $project_name"
    ./gradlew ${project_name}:assembleRelease -q
    if [ $? -ne 0 ];then
        echo "Failed !"
        exit -1
    fi
    echo "==== Success !"
    echo "=========================================================="
    echo "  "
done;

echo "  "
echo "  "
echo "=========================================================="
echo "==== Uploading all project ..."
echo "=========================================================="
echo "  "
echo "  "

########## 上传产物 ##########
for project_name in ${core_array[@]}
do
    echo "  "
    echo "  "
    echo "=========================================================="
    echo "==== Uploading $project_name"
    ./gradlew ${project_name}:publish -q
    if [ $? -ne 0 ];then
        echo "Failed !"
        exit -1
    fi
    echo "==== Success !"
    echo "=========================================================="
    echo "  "
done;


version_t=$(cat gradle.properties | grep POM_VERSION_NAME)
version=${version_t//"POM_VERSION_NAME="/""}

echo "  "
echo "  "
echo "=========================================================="
echo "==== "
echo "==== All project been publish successful. "
echo "==== "
echo "==== Version: ${version}"
echo "==== "
echo "=========================================================="